import { createSlice } from "@reduxjs/toolkit";
import axios from 'axios'

const billStore = createSlice({
    name:'bill',
    initialState:{
        billList:[]
    },
    reducers:{
        //同步修改
       setBillList(state,action){
           state.billList = action.payload
       },
       //添加一笔账单
       addBill(state,action){
           state.billList.push(action.payload)
       }
    }
})
//异步修改
const getBillList = ()=>{
    return async(dispatch)=>{
       const res = await axios.get('http://localhost:8888/ka')
       dispatch(setBillList(res.data))
    }
}
const getAddBill =(data)=>{
     return async(dispatch)=>{
        const res = await axios.post('http://localhost:8888/ka',data)
        dispatch(addBill(res.data))
     }
}

const {setBillList,addBill}=billStore.actions

export {setBillList,getBillList,getAddBill}
export default billStore.reducer